﻿using FirstFloor.ModernUI.Presentation;
using SelElectCalcModernUI.Model;
using SelElectCalcModernUI.Service;
using SelElectCalcModernUI.ViewModel.CalcRes;
using SelElectCalcModernUI.ViewModel.Home;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace SelElectCalcModernUI.ViewModel
{
    public class HomeViewModel
        : NotifyPropertyChanged, IDisposable
    {
        HomeDataModel homeData;
        CommandBase deleteFactoriesCmd;
        CommandBase addFactoriesCmd;
        CommandBase deleteSellersCmd;
        CommandBase addSellersCmd;
        CommandBase calcCmd;
        PowerFactory editingFactory;
        PowerSeller editingSeller;
        int addCount = 3;

        public HomeDataModel HomeData
        {
            get
            {
                if (homeData == null)
                    homeData = new HomeDataModel();
                return homeData;
            }

            set
            {
                homeData = value;
            }
        }

        public CommandBase CalcCmd
        {
            get
            {
                if (calcCmd == null)
                    calcCmd = new RelayCommand((s) =>
                    {
                        var flist = HomeData.PowerFactories.Select((ss) =>
                        {
                            return new FactoryModel()
                            {
                                Accu_money = 0,
                                Accu_money_diff = 0,
                                Energy_left = ss.Power,
                                Name = ss.Name,
                                Power = ss.Power,
                                Price = HomeData.StandardFactoryPrice + ss.Price / 1000,
                                Times = ss.Times
                            };
                        }).ToList();
                        var slist = HomeData.PowerSellers.Select((ss) =>
                        {
                            return new SellerModel()
                            {

                                Accu_money = 0,
                                Accu_money_diff = 0,
                                Energy_left = ss.Power,
                                Name = ss.Name,
                                Power = ss.Power,
                                Price = HomeData.StandardSellerPrice + ss.Price / 1000,
                                Times = ss.Times
                            };
                        }).ToList();
                        var res = CalcService.Run(HomeData.TotalPower, HomeData.TurnoverRatio / 100, flist, slist);
                        ViewModelLocator.AllDeal.SetDealData(res);
                        IEnumerable<FactoryResSignle> factories = CalcService.GetFactories(res, ViewModelLocator.OtherSetting.FactoryBackRage / 100);
                        IEnumerable<SellerResSignle> sellers = CalcService.GetSellers(res, ViewModelLocator.OtherSetting.SellerBackRage / 100);
                        IEnumerable<FactoryResSignle> factoriesDetail = CalcService.GetFactoriesDetail(res, ViewModelLocator.OtherSetting.FactoryBackRage / 100);
                        IEnumerable<SellerResSignle> sellersDetail = CalcService.GetSellersDetail(res, ViewModelLocator.OtherSetting.SellerBackRage / 100);
                        ViewModelLocator.FactoryResult.FactoryResData.FactoryRes.Clear();
                        ViewModelLocator.SellerResult.SellerResData.SellerRes.Clear();
                        foreach (var item in factories)
                            ViewModelLocator.FactoryResult.FactoryResData.FactoryRes.Add(item);
                        foreach (var item in sellers)
                            ViewModelLocator.SellerResult.SellerResData.SellerRes.Add(item);
                        foreach (var item in factoriesDetail)
                            ViewModelLocator.FactoryResult.FactoryResData.FactoryResDetail.Add(item);
                        foreach (var item in sellersDetail)
                            ViewModelLocator.SellerResult.SellerResData.SellerResDetail.Add(item);
                        ViewModelLocator.FactoryResult.FactoryResFilter = "全部";
                        ViewModelLocator.SellerResult.SellerResFilter = "全部";
                        ViewModelLocator.JumpPageEvent("/Pages/CalcResPage.xaml");
                    }, (s) => HomeData.PowerFactories != null &&
                    HomeData.PowerFactories.Count > 0 &&
                    HomeData.PowerSellers != null &&
                    HomeData.PowerSellers.Count > 0);
                return calcCmd;
            }
        }


        public PowerFactory EditingFactory
        {
            get
            {
                if (editingFactory == null)
                    editingFactory = new PowerFactory();
                return editingFactory;
            }

            set
            {
                editingFactory = value;
                OnPropertyChanged("EditingFactory");
            }
        }

        public PowerSeller EditingSeller
        {
            get
            {
                if (editingSeller == null)
                    editingSeller = new PowerSeller();
                return editingSeller;
            }

            set
            {
                editingSeller = value;
                OnPropertyChanged("EditingSeller");
            }
        }

        public CommandBase DeleteFactoriesCmd
        {
            get
            {
                if (deleteFactoriesCmd == null)
                    deleteFactoriesCmd = new RelayCommand((s) =>
                    {
                        if (s is ObservableCollection<object>)
                        {
                            var list = (s as ObservableCollection<object>).ToList();
                            foreach (var item in list)
                            {
                                if (item is PowerFactory)
                                    HomeData.PowerFactories.Remove(item as PowerFactory);
                            }
                        }
                    });
                return deleteFactoriesCmd;
            }
        }

        public CommandBase AddFactoriesCmd
        {
            get
            {
                if (addFactoriesCmd == null)
                    addFactoriesCmd = new RelayCommand((s) =>
                    {
                        if (!ViewModelLocator.DialogShowEvent("/Pages/AddFactories.xaml"))
                            return;
                        var maxfactory = GetFactoryExist();
                        int maxTimes = 0;
                        if (maxfactory != null)
                            maxTimes = maxfactory.Times;
                        if (AddCount > 0)
                        {
                            PowerFactory factory = null;
                            for (int i = 0; i < AddCount; i++)
                            {
                                factory = EditingFactory.Copy();
                                factory.Times = maxTimes + i;
                                HomeData.PowerFactories.Add(factory);
                            }
                        }
                    });
                return addFactoriesCmd;
            }
        }

        public CommandBase DeleteSellersCmd
        {
            get
            {
                if (deleteSellersCmd == null)
                    deleteSellersCmd = new RelayCommand((s) =>
                    {
                        if (s is ObservableCollection<object>)
                        {
                            var list = (s as ObservableCollection<object>).ToList();
                            foreach (var item in list)
                            {
                                if (item is PowerSeller)
                                    HomeData.PowerSellers.Remove(item as PowerSeller);
                            }
                        }
                    });
                return deleteSellersCmd;
            }
        }

        public CommandBase AddSellersCmd
        {
            get
            {
                if (addSellersCmd == null)
                    addSellersCmd = new RelayCommand((s) =>
                    {
                        if (!ViewModelLocator.DialogShowEvent("/Pages/AddSellers.xaml"))
                            return;
                        var maxseller = GetSellerExist();
                        int maxTimes = 0;
                        if (maxseller != null)
                            maxTimes = maxseller.Times;
                        if (AddCount > 0)
                        {
                            PowerSeller seller = null;
                            for (int i = 0; i < AddCount; i++)
                            {
                                seller = EditingSeller.Copy();
                                seller.Times = maxTimes + i;
                                HomeData.PowerSellers.Add(seller);
                            }
                        }
                    });
                return addSellersCmd;
            }
        }

        public int AddCount
        {
            get
            {
                return addCount;
            }

            set
            {
                OnPropertyChanged("AddCount");
                addCount = value;
            }
        }

        public HomeViewModel()
        {
            this.homeData = SaveLoadData.LoadCalcData<HomeDataModel>();
        }

        private PowerSeller GetSellerExist()
        {
            var list = (from c in HomeData.PowerSellers where c.Name.Equals(editingSeller.Name) orderby c.Times descending select c);
            if (list.Count() <= 0)
                return null;
            else
                return list.First();
        }

        private PowerFactory GetFactoryExist()
        {
            var list = (from c in HomeData.PowerFactories where c.Name.Equals(editingFactory.Name) orderby c.Times descending select c);
            if (list.Count() <= 0)
                return null;
            else
                return list.First();
        }

        public void Dispose()
        {
            SaveLoadData.SaveCalcData(this.homeData);
        }
    }
}
